<?php
namespace app\admin\controller;
use think\Db;
class Count extends Common
{
	//	还款单数 渠道详情页面
	public function qudao_money(){
		$times= input('ID');//日期
    	$this->assign('times',$times);
    	return $this->fetch();
	}
//	还款单数 渠道详情页面
	public function qudao_danzi(){
		$times= input('ID');//日期
    	$this->assign('times',$times);
    	return $this->fetch();
	}
//	还款金额 渠道详情列表
	public function qudao_moneyss(){
		 $page=input('page');
         $limit= input('limit');
         $re=Db::table('xb_tg_admin')->select();
	     $cuont=count($re);
	     songsong($re,$cuont);
    	
	
	}
//	还款单数 渠道详情列表
	public function qudao_danziss(){
		 $times= input('times');//日期
		 $page=input('page');
         $limit= input('limit');
         $re=Db::table('xb_tg_admin')->Field('Name,ID')->select();
//		 P($re);
		 foreach ($re as $k => $v) {
		 	$re[$k]['times']=$times;//日期
		 	$Time1=$times.' 00:00:00';
			$Time2=$times.' 23:59:59';
//       	应还单数统计
	        $where1['TgadminID']=$v['ID'];
			$where2['TgadminID']=$v['ID'];
			$where1['YyFkTime']=['between',[$Time1,$Time2]];
			$where2['YyFkTime2']=['between',[$Time1,$Time2]];
			$count1=Db::table('xb_loans_applylist a')->where($where1)
			->join('xb_mem_info b','a.UserID=b.ID','LEFT')
			->join('xb_tg_admin c','b.TgadminID=c.ID',"LEFT")
			->field('a.ID,a.OrderSn,a.LoanStatus')->select();			
			$count2=Db::table('xb_loans_applylist a')->where($where2)
			->join('xb_mem_info b','a.UserID=b.ID','LEFT')
			->join('xb_tg_admin c','b.TgadminID=c.ID',"LEFT")
			->field('a.ID,a.OrderSn,a.LoanStatus')->select();
			$count=array_merge($count1,$count2);
			$count=assoc_unique($count, 'ID');//根据订单ID 去重
			$re[$k]['yinghuan_count']=count($count);//应还单数=当日期到期应还+续期以后当日应还
			
//			P($re[$k]['yinghuan_count']);
//          全额单数
			$quaner_count='0';
			foreach ($count as $kk => $vv) {
				if($vv['LoanStatus']=='3'){
					$quaner_count=$quaner_count+1;
				}
			}
			$re[$k]['quaner_count']=$quaner_count;//全额还款单数=当日还全款的单数
//          续期单数
			$xuqi_count='0';
			foreach ($count as $kkk => $vvv) {
				if($vvv['LoanStatus']=='2'){
					 $StartTime= strtotime($v['times']);
					 $time=86400*6;
					 $StartTime=$StartTime+$time;
					 $YyFkTime=date("Y-m-d H:i:s",$StartTime);
					 $data=Db::table('xb_loans_xjapplylist')->where(['OrderSn'=>$vvv['OrderSn'],'YyFkTime'=>$v['times']])->find();
					 if($data){
					  $xuqi_count=$xuqi_count+1;
					 }
				}
			}
			$re[$k]['xuqi_count']=$xuqi_count;//展期单数=当日展期单数
//			实际还款单数
            $shiji_cont=$re[$k]['quaner_count']+$re[$k]['xuqi_count'];
			$re[$k]['shiji_cont']=$shiji_cont;//实还单数=续期单数+全额单数
//          还款率
			$re[$k]['huankuanlv']=lilv($re[$k]['shiji_cont'],$re[$k]['yinghuan_count']);
			
//			未还单数
            $weihuan_count=$re[$k]['yinghuan_count']-$re[$k]['shiji_cont'];
			$re[$k]['weihuan_count']=$weihuan_count;//未还单数=应还单数-实还单数
//          全款率
            $re[$k]['quankuanlv']=lilv($re[$k]['quaner_count'],$re[$k]['shiji_cont']);

//          续期率
            if($re[$k]['shiji_cont']<=0){
				$re[$k]['xuqilv']='0.00%';
		    }else{
               $quankuanlv=$re[$k]['xuqi_count']/$re[$k]['shiji_cont'];
			   $re[$k]['xuqilv']=$quankuanlv;//续期率=续期单数/实还单数
			   if(strlen($re[$k]['xuqilv'])>5){
				  $re[$k]['xuqilv']=substr($re[$k]['xuqilv'] , 0 , 5);
				}
			   $re[$k]['xuqilv']=$re[$k]['xuqilv']*100;
			   if($re[$k]['xuqilv']=='0'){
			   	$re[$k]['xuqilv']='0.00%';
			   }else{
			    $re[$k]['xuqilv']=$re[$k]['xuqilv'].'%';
			   }
			}
			 $re[$k]['xuqilv']=lilv($re[$k]['xuqi_count'],$re[$k]['shiji_cont']);
			
		 }
//      新客统计
        foreach ($re as $k => $v) {
//       	应还单数统计
        	$Time1=$v['times'].' 00:00:00';
			$Time2=$v['times'].' 23:59:59';
			$where1['YyFkTime']=['between',[$Time1,$Time2]];
			$where1['user_type']='1';//新客户
			$where2['YyFkTime2']=['between',[$Time1,$Time2]];
			$where2['user_type']='1';//客户类型1 新客 2 老客
			$count1=Db::table('xb_loans_applylist a')
			->field('a.ID,a.OrderSn,a.LoanStatus')
            ->where($where1)
		    ->join('xb_mem_info b','a.UserID=b.ID','LEFT')
			->join('xb_tg_admin c','b.TgadminID=c.ID',"LEFT")
		    ->select(); //当日期到期应还
			$count2=Db::table('xb_loans_applylist a')
			->field('a.ID,a.OrderSn,a.LoanStatus')
            ->where($where2)
		    ->join('xb_mem_info b','a.UserID=b.ID','LEFT')
			->join('xb_tg_admin c','b.TgadminID=c.ID',"LEFT")
		    ->select();//续期以后当日应还
			$count=array_merge($count1,$count2);
			$count=assoc_unique($count, 'ID');//根据订单ID 去重
			$re[$k]['xinke_yinghuan_count']=count($count);//应还单数=当日期到期应还+续期以后当日应还
//          全额单数
			$xinke_quaner_count='0';
			foreach ($count as $kk => $vv) {
				if($vv['LoanStatus']=='3'){
					$xinke_quaner_count=$xinke_quaner_count+1;
				}
			}
			$re[$k]['xinke_quaner_count']=$xinke_quaner_count;//全额还款单数=当日还全款的单数
//          续期单数
			$xinke_xuqi_count='0';
			foreach ($count as $kkk => $vvv) {
				if($vvv['LoanStatus']=='2'){
					 $StartTime= strtotime($v['times']);
					 $time=86400*6;
					 $StartTime=$StartTime+$time;
					 $YyFkTime=date("Y-m-d H:i:s",$StartTime);
					 $data=Db::table('xb_loans_xjapplylist')->where(['OrderSn'=>$vvv['OrderSn'],'YyFkTime'=>$v['times']])->find();
					 if($data){
					  $xinke_xuqi_count=$xinke_xuqi_count+1;
					 }
				}
			}
			$re[$k]['xinke_xuqi_count']=$xinke_xuqi_count;//展期单数=当日展期单数
//			实际还款单数
            $xinke_shiji_cont=$re[$k]['xinke_quaner_count']+$re[$k]['xinke_xuqi_count'];
			$re[$k]['xinke_shiji_cont']=$xinke_shiji_cont;//实还单数=续期单数+全额单数
//			未还单数
            $xinke_weihuan_count=$re[$k]['xinke_yinghuan_count']-$re[$k]['xinke_shiji_cont'];
			$re[$k]['xinke_weihuan_count']=$xinke_weihuan_count;//未还单数=应还单数-实还单数
			
//			还款率    还款单数/应还单数
            $re[$k]['xinke_huankuanlv']=lilv($re[$k]['xinke_shiji_cont'],$re[$k]['xinke_yinghuan_count']);
			
        }


//      老客统计
        foreach ($re as $k => $v) {
//       	应还单数统计
        	$Time1=$v['times'].' 00:00:00';
			$Time2=$v['times'].' 23:59:59';
			$where1['YyFkTime']=['between',[$Time1,$Time2]];
			$where1['user_type']='2';//客户类型1 新客 2 老客   
			$where2['YyFkTime2']=['between',[$Time1,$Time2]];
			$where2['user_type']='2';//客户类型1 新客 2 老客   
			$count1=Db::table('xb_loans_applylist a')
			->field('a.ID,a.OrderSn,a.LoanStatus')
            ->where($where1)
		    ->join('xb_mem_info b','a.UserID=b.ID','LEFT')
			->join('xb_tg_admin c','b.TgadminID=c.ID',"LEFT")
		    ->select();
			$count2=Db::table('xb_loans_applylist a')
			->field('a.ID,a.OrderSn,a.LoanStatus')
            ->where($where2)
		    ->join('xb_mem_info b','a.UserID=b.ID','LEFT')
			->join('xb_tg_admin c','b.TgadminID=c.ID',"LEFT")
		    ->select();
			$count=array_merge($count1,$count2);
			$count=assoc_unique($count, 'ID');//根据订单ID 去重
			$re[$k]['laoke_yinghuan_count']=count($count);//应还单数=当日期到期应还+续期以后当日应还
		
//          全额单数
			$laoke_quaner_count='0';
			foreach ($count as $kk => $vv) {
				if($vv['LoanStatus']=='3'){
					$laoke_quaner_count=$laoke_quaner_count+1;
				}
			}
			$re[$k]['laoke_quaner_count']=$laoke_quaner_count;//全额还款单数=当日还全款的单数
//          续期单数
			$laoke_xuqi_count='0';
			foreach ($count as $kkk => $vvv) {
				if($vvv['LoanStatus']=='2'){
					 $StartTime= strtotime($v['times']);
					 $time=86400*6;
					 $StartTime=$StartTime+$time;
					 $YyFkTime=date("Y-m-d H:i:s",$StartTime);
					 $data=Db::table('xb_loans_xjapplylist')->where(['OrderSn'=>$vvv['OrderSn'],'YyFkTime'=>$v['times']])->find();
					 if($data){
					  $laoke_xuqi_count=$laoke_xuqi_count+1;
					 }
				}
			}
			$re[$k]['laoke_xuqi_count']=$laoke_xuqi_count;//展期单数=当日展期单数
//			实际还款单数
            $laoke_shiji_cont=$re[$k]['laoke_quaner_count']+$re[$k]['laoke_xuqi_count'];
			$re[$k]['laoke_shiji_cont']=$laoke_shiji_cont;//实还单数=续期单数+全额单数
			//			未还单数
            $laoke_weihuan_count=$re[$k]['laoke_yinghuan_count']-$re[$k]['laoke_shiji_cont'];
			$re[$k]['laoke_weihuan_count']=$laoke_weihuan_count;//未还单数=应还单数-实还单数.
//			还款率    还款单数/应还单数
            $re[$k]['laoke_huankuanlv']=lilv($re[$k]['laoke_shiji_cont'],$re[$k]['laoke_yinghuan_count']);
        }
		 
		

//		 p($re);die;
	     $cuont=count($re);
	     songsong($re,$cuont);
	}
	
//	还款统计页面
	public function huankuantongji(){
		
	return $this->fetch();
	}
//	还款统计列表数据
	public function get_list_huankuan_dan(){
		 $page=input('page');
         $limit= input('limit');
		 $ApplyTime=input('ApplyTime');//日期选择
		 $where=[];
		 if($ApplyTime){
			$ApplyTime=explode(' - ', $ApplyTime);
	        $EndTime= strtotime($ApplyTime['1']);
				 $StartTime= strtotime($ApplyTime['0']);
				 $days=$EndTime-$StartTime;
				 $days=$days/86400;
				 $days=$days+1;
				 $time=$ApplyTime['1'];
		 }else{
		 	$time=date("Y-m-d");
			$days='10';
		    $time=date("Y-m-d");
		 }
         $re=dates_range1($time,$days);
//		 总的统计
         foreach ($re as $k => $v) {
//       	应还单数统计
        	$Time1=$v['times'].' 00:00:00';
			$Time2=$v['times'].' 23:59:59';
			$where1['YyFkTime']=['between',[$Time1,$Time2]];
			$where2['YyFkTime2']=['between',[$Time1,$Time2]];
			$count1=Db::table('xb_loans_applylist')->where($where1)->field('ID,OrderSn,LoanStatus')->select();
			$count2=Db::table('xb_loans_applylist')->where($where2)->field('ID,OrderSn,LoanStatus')->select();
			$count=array_merge($count1,$count2);
			$count=assoc_unique($count, 'ID');//根据订单ID 去重
			$re[$k]['yinghuan_count']=count($count);//应还单数=当日期到期应还+续期以后当日应还
//          全额单数
			$quaner_count='0';
			foreach ($count as $kk => $vv) {
				if($vv['LoanStatus']=='3'){
					$quaner_count=$quaner_count+1;
				}
			}
			$re[$k]['quaner_count']=$quaner_count;//全额还款单数=当日还全款的单数
//          续期单数
			$xuqi_count='0';
			foreach ($count as $kkk => $vvv) {
				if($vvv['LoanStatus']=='2'){
					 $StartTime= strtotime($v['times']);
					 $time=86400*6;
					 $StartTime=$StartTime+$time;
					 $YyFkTime=date("Y-m-d H:i:s",$StartTime);
					 $data=Db::table('xb_loans_xjapplylist')->where(['OrderSn'=>$vvv['OrderSn'],'YyFkTime'=>$v['times']])->find();
					 if($data){
					  $xuqi_count=$xuqi_count+1;
					 }
				}
			}
			$re[$k]['xuqi_count']=$xuqi_count;//展期单数=当日展期单数
//			实际还款单数
            $shiji_cont=$re[$k]['quaner_count']+$re[$k]['xuqi_count'];
			$re[$k]['shiji_cont']=$shiji_cont;//实还单数=续期单数+全额单数
//          还款率
			$re[$k]['huankuanlv']=lilv($re[$k]['shiji_cont'],$re[$k]['yinghuan_count']);
			
//			未还单数
            $weihuan_count=$re[$k]['yinghuan_count']-$re[$k]['shiji_cont'];
			$re[$k]['weihuan_count']=$weihuan_count;//未还单数=应还单数-实还单数
//          全款率
            $re[$k]['quankuanlv']=lilv($re[$k]['quaner_count'],$re[$k]['shiji_cont']);

//          续期率
            if($re[$k]['shiji_cont']<=0){
				$re[$k]['xuqilv']='0.00%';
		    }else{
               $quankuanlv=$re[$k]['xuqi_count']/$re[$k]['shiji_cont'];
			   $re[$k]['xuqilv']=$quankuanlv;//续期率=续期单数/实还单数
			   if(strlen($re[$k]['xuqilv'])>5){
				  $re[$k]['xuqilv']=substr($re[$k]['xuqilv'] , 0 , 5);
				}
			   $re[$k]['xuqilv']=$re[$k]['xuqilv']*100;
			   if($re[$k]['xuqilv']=='0'){
			   	$re[$k]['xuqilv']='0.00%';
			   }else{
			    $re[$k]['xuqilv']=$re[$k]['xuqilv'].'%';
			   }
			}
			
			 $re[$k]['xuqilv']=lilv($re[$k]['xuqi_count'],$re[$k]['shiji_cont']);
			
			
        }
//p($re);die;
//      新客统计
        foreach ($re as $k => $v) {
//       	应还单数统计
        	$Time1=$v['times'].' 00:00:00';
			$Time2=$v['times'].' 23:59:59';
			$where1['YyFkTime']=['between',[$Time1,$Time2]];
			$where1['user_type']='1';//新客户
			$where2['YyFkTime2']=['between',[$Time1,$Time2]];
			$where2['user_type']='1';//客户类型1 新客 2 老客
			$count1=Db::table('xb_loans_applylist a')
			->field('a.ID,a.OrderSn,a.LoanStatus')
            ->where($where1)
		    ->join('xb_mem_info b','a.UserID=b.ID','LEFT')
		    ->select(); //当日期到期应还
			$count2=Db::table('xb_loans_applylist a')
			->field('a.ID,a.OrderSn,a.LoanStatus')
            ->where($where2)
		    ->join('xb_mem_info b','a.UserID=b.ID','LEFT')
		    ->select();//续期以后当日应还
			$count=array_merge($count1,$count2);
			$count=assoc_unique($count, 'ID');//根据订单ID 去重
			$re[$k]['xinke_yinghuan_count']=count($count);//应还单数=当日期到期应还+续期以后当日应还
//          全额单数
			$xinke_quaner_count='0';
			foreach ($count as $kk => $vv) {
				if($vv['LoanStatus']=='3'){
					$xinke_quaner_count=$xinke_quaner_count+1;
				}
			}
			$re[$k]['xinke_quaner_count']=$xinke_quaner_count;//全额还款单数=当日还全款的单数
//          续期单数
			$xinke_xuqi_count='0';
			foreach ($count as $kkk => $vvv) {
				if($vvv['LoanStatus']=='2'){
					 $StartTime= strtotime($v['times']);
					 $time=86400*6;
					 $StartTime=$StartTime+$time;
					 $YyFkTime=date("Y-m-d H:i:s",$StartTime);
					 $data=Db::table('xb_loans_xjapplylist')->where(['OrderSn'=>$vvv['OrderSn'],'YyFkTime'=>$v['times']])->find();
					 if($data){
					  $xinke_xuqi_count=$xinke_xuqi_count+1;
					 }
				}
			}
			$re[$k]['xinke_xuqi_count']=$xinke_xuqi_count;//展期单数=当日展期单数
//			实际还款单数
            $xinke_shiji_cont=$re[$k]['xinke_quaner_count']+$re[$k]['xinke_xuqi_count'];
			$re[$k]['xinke_shiji_cont']=$xinke_shiji_cont;//实还单数=续期单数+全额单数
//			未还单数
            $xinke_weihuan_count=$re[$k]['xinke_yinghuan_count']-$re[$k]['xinke_shiji_cont'];
			$re[$k]['xinke_weihuan_count']=$xinke_weihuan_count;//未还单数=应还单数-实还单数
			
//			还款率    还款单数/应还单数
            $re[$k]['xinke_huankuanlv']=lilv($re[$k]['xinke_shiji_cont'],$re[$k]['xinke_yinghuan_count']);
			
        }


//      老客统计
        foreach ($re as $k => $v) {
//       	应还单数统计
        	$Time1=$v['times'].' 00:00:00';
			$Time2=$v['times'].' 23:59:59';
			$where1['YyFkTime']=['between',[$Time1,$Time2]];
			$where1['user_type']='2';//客户类型1 新客 2 老客   
			$where2['YyFkTime2']=['between',[$Time1,$Time2]];
			$where2['user_type']='2';//客户类型1 新客 2 老客   
			$count1=Db::table('xb_loans_applylist a')
			->field('a.ID,a.OrderSn,a.LoanStatus')
            ->where($where1)
		    ->join('xb_mem_info b','a.UserID=b.ID','LEFT')
		    ->select();
			$count2=Db::table('xb_loans_applylist a')
			->field('a.ID,a.OrderSn,a.LoanStatus')
            ->where($where2)
		    ->join('xb_mem_info b','a.UserID=b.ID','LEFT')
		    ->select();
			$count=array_merge($count1,$count2);
			$count=assoc_unique($count, 'ID');//根据订单ID 去重
			$re[$k]['laoke_yinghuan_count']=count($count);//应还单数=当日期到期应还+续期以后当日应还
		
//          全额单数
			$laoke_quaner_count='0';
			foreach ($count as $kk => $vv) {
				if($vv['LoanStatus']=='3'){
					$laoke_quaner_count=$laoke_quaner_count+1;
				}
			}
			$re[$k]['laoke_quaner_count']=$laoke_quaner_count;//全额还款单数=当日还全款的单数
//          续期单数
			$laoke_xuqi_count='0';
			foreach ($count as $kkk => $vvv) {
				if($vvv['LoanStatus']=='2'){
					 $StartTime= strtotime($v['times']);
					 $time=86400*6;
					 $StartTime=$StartTime+$time;
					 $YyFkTime=date("Y-m-d H:i:s",$StartTime);
					 $data=Db::table('xb_loans_xjapplylist')->where(['OrderSn'=>$vvv['OrderSn'],'YyFkTime'=>$v['times']])->find();
					 if($data){
					  $laoke_xuqi_count=$laoke_xuqi_count+1;
					 }
				}
			}
			$re[$k]['laoke_xuqi_count']=$laoke_xuqi_count;//展期单数=当日展期单数
//			实际还款单数
            $laoke_shiji_cont=$re[$k]['laoke_quaner_count']+$re[$k]['laoke_xuqi_count'];
			$re[$k]['laoke_shiji_cont']=$laoke_shiji_cont;//实还单数=续期单数+全额单数
			//			未还单数
            $laoke_weihuan_count=$re[$k]['laoke_yinghuan_count']-$re[$k]['laoke_shiji_cont'];
			$re[$k]['laoke_weihuan_count']=$laoke_weihuan_count;//未还单数=应还单数-实还单数.
//			还款率    还款单数/应还单数
            $re[$k]['laoke_huankuanlv']=lilv($re[$k]['laoke_shiji_cont'],$re[$k]['laoke_yinghuan_count']);
        }
//p($re);die;
		$cuont=count($re);
        $star=($page-1)*$limit;//开始截取位置
		$re=array_slice($re,$star,$limit);//分页处理
	    songsong($re,$cuont);
	}

//	还款统计金额
	public function get_list_huankuan_money(){
		 $page=input('page');
         $limit= input('limit');
         //搜索条件
         $StartTime=input('StartTime');
		 $EndTime=input('EndTime');
		 if($StartTime && $EndTime){
		 	if($EndTime>=$StartTime){
				$time=$EndTime;
		 		$EndTime= strtotime($EndTime);
				 $StartTime= strtotime($StartTime);
				 $days=$EndTime-$StartTime;
				 $days=$days/86400;
				 $days=$days+1;
//				 p($days);die;
				 if($days<=0){
				 	$days='10';
				 }
		 	}else{
		 		$time=date("Y-m-d");
			    $days='10';
		 	}
		 }else{
		 	$time=date("Y-m-d");
			$days='10';
		 }
         $re=dates_range1($time,$days);
         foreach ($re as $k => $v) {
//          出账统计  订单表中计算放放款总金额就可以了
        	$Time1=$v['times'].' 00:00:00';
			$Time2=$v['times'].' 23:59:59';
			$where_xb_loans_applylist_laoke['OpenTime']=['between',[$Time1,$Time2]];
			$xb_loans_applylist_count_laoke=Db::table('xb_loans_applylist')->where($where_xb_loans_applylist_laoke)->field('AdoptMoney,pay,ApplyMoney')->select();
			if($xb_loans_applylist_count_laoke){
				$chuzhang='0';//总
				$chuzhang_weixin='0';//微信
				$chuzhang_zhifubao='0';//支付宝
				$chuzhang_yinhang='0';//银行
				foreach ($xb_loans_applylist_count_laoke as $kk => $vv) {
					if($vv['pay']=='1'){
						$chuzhang_zhifubao=$chuzhang_zhifubao+$vv['AdoptMoney'];
					}
					if($vv['pay']=='2'){
						$chuzhang_zhifubao=$chuzhang_zhifubao+$vv['AdoptMoney'];
					}
					if($vv['pay']=='3'){
						$chuzhang_yinhang=$chuzhang_yinhang+$vv['AdoptMoney'];
					}
					$chuzhang=$chuzhang+$vv['AdoptMoney'];
				}
				$re[$k]['chuzhang']=$chuzhang.'元';
				if($chuzhang_weixin>0){
					$re[$k]['chuzhang_weixin']=$chuzhang_weixin.'元';
				}else{
					$re[$k]['chuzhang_weixin']='0元';
				}
				if($chuzhang_zhifubao>0){
					$re[$k]['chuzhang_zhifubao']=$chuzhang_zhifubao.'元';
				}else{
					$re[$k]['chuzhang_zhifubao']='0元';
				}
				if($chuzhang_yinhang>0){
					$re[$k]['chuzhang_yinhang']=$chuzhang_yinhang.'元';
				}else{
					$re[$k]['chuzhang_yinhang']='0元';
				}
			}else{
				$re[$k]['chuzhang']='0元';
				$re[$k]['chuzhang_weixin']='0元';
				$re[$k]['chuzhang_zhifubao']='0元';
				$re[$k]['chuzhang_yinhang']='0元';
			}
//			进账统计  支付记录表计算总金额就可以了
			$where_xb_loans_paylist['CreateTime']=['between',[$Time1,$Time2]];
			$xbloans_paylist=Db::table('xb_loans_paylist')->where($where_xb_loans_paylist)->field('OrderAmount,PayType')->select();
			if($xbloans_paylist){
//				p($xbloans_paylist);
				$jinzhang='0';//总
				$jinzhang_weixin='0';//线下微信
				$jinzhang_zhifubao='0';//线下支付宝
				$jinzhang_yinhang='0';//线下银行
				$jinzhang_yinhang2='0';//线上银行
				foreach ($xbloans_paylist as $aa => $bb) {
					if($bb['PayType']=='1'){
						$jinzhang_weixin=$jinzhang_weixin+$bb['OrderAmount'];
					}
					if($bb['PayType']=='2'){
						$jinzhang_zhifubao=$jinzhang_zhifubao+$bb['OrderAmount'];
					}
					if($bb['PayType']=='3'){
						$jinzhang_yinhang=$jinzhang_yinhang+$bb['OrderAmount'];
					}
					if($bb['PayType']=='4'){
						$jinzhang_yinhang2=$jinzhang_yinhang2+$bb['OrderAmount'];
					}
					$jinzhang=$jinzhang+$bb['OrderAmount'];
				}
				$re[$k]['jinzhang']=$jinzhang.'元';
				$re[$k]['jinzhang_weixin']=$jinzhang_weixin.'元';
				$re[$k]['jinzhang_zhifubao']=$jinzhang_zhifubao.'元';
				$re[$k]['jinzhang_yinhang']=$jinzhang_yinhang.'元';
				$re[$k]['jinzhang_yinhang2']=$jinzhang_yinhang2.'元';
			}else{
				$re[$k]['jinzhang']='0元';
				$re[$k]['jinzhang_weixin']='0元';
				$re[$k]['jinzhang_zhifubao']='0元';
				$re[$k]['jinzhang_yinhang']='0元';
				$re[$k]['jinzhang_yinhang2']='0元';
			}
        }
        $data_re=$re;
		$cuont=count($re);
		$star=($page-1)*$limit;//开始截取位置
		$re=array_slice($data_re,$star,$limit);
        $jinzhang_zong='0';
		$jinzhang_zong_weixin='0';
		$jinzhang_zong_zhifubao='0';
	    $jinzhang_zong_yinhang='0';
		$jinzhang_zong_yinhang2='0';
     	$chuzhang_zong='0';
		$chuzhang_zong_weixin='0';
		$chuzhang_zong_zhifubao='0';
		$chuzhang_zong_yinhang='0';
//		p($data_re);die;
		foreach ($data_re as $k => $v) {
			$jinzhang_zong=$jinzhang_zong+$v['jinzhang'];
			$jinzhang_zong_weixin=$jinzhang_zong_weixin+$v['jinzhang_weixin'];
			$jinzhang_zong_zhifubao=$jinzhang_zong_zhifubao+$v['jinzhang_zhifubao'];
			$jinzhang_zong_yinhang=$jinzhang_zong_yinhang+$v['jinzhang_yinhang'];
			$jinzhang_zong_yinhang2=$jinzhang_zong_yinhang2+$v['jinzhang_yinhang2'];
			$chuzhang_zong=$chuzhang_zong+$v['chuzhang'];
			$chuzhang_zong_weixin=$chuzhang_zong_weixin+$v['chuzhang_weixin'];
			$chuzhang_zong_zhifubao=$chuzhang_zong_zhifubao+$v['chuzhang_zhifubao'];
			$chuzhang_zong_yinhang=$chuzhang_zong_yinhang+$v['chuzhang_yinhang'];
		}
		$zhuijia['jinzhang']=$jinzhang_zong.'元';
		$zhuijia['jinzhang_weixin']=$jinzhang_zong_weixin.'元';
		$zhuijia['jinzhang_zhifubao']=$jinzhang_zong_zhifubao.'元';
        $zhuijia['jinzhang_yinhang']=$jinzhang_zong_yinhang.'元';
		$zhuijia['jinzhang_yinhang2']=$jinzhang_zong_yinhang2.'元';
		$zhuijia['chuzhang']=$chuzhang_zong.'元';
		$zhuijia['chuzhang_weixin']=$chuzhang_zong_weixin.'元';
		$zhuijia['chuzhang_zhifubao']=$chuzhang_zong_zhifubao.'元';
		$zhuijia['chuzhang_yinhang']=$chuzhang_zong_yinhang.'元';
		$zhuijia['times']='查询时间段总计';
        $re[]=$zhuijia;
		songsong($re,$cuont);
	}
	
	
//    用户列表
    public function default()
    {
		$ID=session('id');//通过session获取用户ID
		$where['ID']=$ID;
		$RoleID=Db::table('xb_sys_administrator')->where($where)->value('RoleID');//查询角色id
//		通过角色id查询该角色拥有的权限
        $where1=[];
     	if($RoleID=='1'){
	      $data=Db::table('xb_sys_menu')->select();//查询角色权限
//	      p($data);die;
     	}else{
     		$where1['ID']=$RoleID;
            $MenuID=Db::table('xb_sys_role')->where($where1)->value('MenuID');//查询角色权限
	//		通过权限id去查找菜单
			$$MenuID=explode(",", $MenuID);
			$data=[];
			foreach($$MenuID as $k=>$v){
		    $where2['ID']=$v;
			$data[]=Db::table('xb_sys_menu')->where($where2)->find();//查询角色权限	
			}
     	}
		
		
		
		$data1=[];
		foreach ($data as $k => $v) {
			if($v['ParentID']==0){
				$data1[]=$v;
			}
		}
		foreach ($data1 as $k => $v) {
			foreach ($data as $kk => $vv){
				if($v['ID']==$vv['ParentID']){
					$data1[$k]['menu_baby'][]=$vv;
				}	
			}
		}
//		升序排序  根据OrderBye
		$data1=array_sort($data1, 'OrderBye', $type = 'asc');
		foreach ($data1 as $a => $b) {
			if(isset($b['menu_baby'])){
				foreach ($b['menu_baby'] as $aa => $bb) {
				   $data1[$a]['menu_baby']=array_sort($b['menu_baby'], 'OrderBye', $type = 'asc'); 
				}
			}
		}
		$this->assign('data',$data1);
		$xiabjindd=Db::table('xb_sys_basicinfo')->find();
		$this->assign('xiabjindd',$xiabjindd);
		return $this->fetch();
    }
//	默认页面系统统计  当日数据
	public function index()
    {
//		            注册人数
        	$Time1=date('Y-m-d').' 00:00:00';
			$Time2=date('Y-m-d').' 23:59:59';
        	$where_xb_mem_info['RegTime'] = ['between',[$Time1,$Time2]];
            $xb_mem_info_count=Db::table('xb_mem_info')->where($where_xb_mem_info)->count('ID');
			$data['zhuce_count']=$xb_mem_info_count;//注册人数
//			放宽量
            $wherefangkuan['OpenTime']=['between',[$Time1,$Time2]];
   			$fangkuan_count_data=Db::table('xb_loans_applylist')->where($wherefangkuan)->field('AdoptMoney,ApplyMoney')->select();
   			$data['fangkuan_count']=count($fangkuan_count_data);//注册人数
   			$data['jiekuan_money']='0';
   			$data['shifu_money']='0';
			foreach ($fangkuan_count_data as $k => $v) {
				$data['jiekuan_money']=$data['jiekuan_money']+$v['ApplyMoney'];//借款金额
				$data['shifu_money']=$data['shifu_money']+$v['AdoptMoney'];//实际金额
			}
//          借款申请量
			$where_xb_loans_applylist['ApplyTime']=['between',[$Time1,$Time2]];
   			$xb_loans_applylist_count=Db::table('xb_loans_applylist')->where($where_xb_loans_applylist)->field('AdoptMoney,ApplyMoney')->select();
   			$data['shenqing_count']=count($xb_loans_applylist_count);//借款申请量
   			$data['shenqing_money']='0';
   			foreach ($xb_loans_applylist_count as $k => $v) {
				$data['shenqing_money']=$data['shenqing_money']+$v['ApplyMoney'];//申请金额金额
			}
//			新客申请量
            $where_xb_loans_applylist_xinke['ApplyTime']=['between',[$Time1,$Time2]];
			$where_xb_loans_applylist_xinke['user_type']='1';
   			$xb_loans_applylist_count_xinke=Db::table('xb_loans_applylist')->where($where_xb_loans_applylist_xinke)->field('AdoptMoney,ApplyMoney')->select();
   			$data['shenqing_count_xinke']=count($xb_loans_applylist_count_xinke);//借款申请量
   			$data['shenqing_money_xinke']='0';
   			foreach ($xb_loans_applylist_count_xinke as $k => $v) {
				$data['shenqing_money_xinke']=$data['shenqing_money_xinke']+$v['ApplyMoney'];//新客申请金额金额
			}
			//			老客申请量
            $where_xb_loans_applylist_laoke['ApplyTime']=['between',[$Time1,$Time2]];
			$where_xb_loans_applylist_laoke['user_type']='2';
   			$xb_loans_applylist_count_laoke=Db::table('xb_loans_applylist')->where($where_xb_loans_applylist_laoke)->field('AdoptMoney,ApplyMoney')->select();
   			$data['shenqing_count_laoke']=count($xb_loans_applylist_count_laoke);//借款申请量
   			$data['shenqing_money_laoke']='0';
   			foreach ($xb_loans_applylist_count_laoke as $k => $v) {
				$data['shenqing_money_laoke']=$data['shenqing_money_laoke']+$v['ApplyMoney'];//老客申请金额金额
			}
		    $this->assign('data',$data);
		    return $this->fetch();
    }
//	认证统计
	public function index1()
    {
    	$qudao=$this->qudao();//调用渠道数据
		$this->assign('qudao',$qudao);
		return $this->fetch();
    }
// 	进出账统计
    public function index2()
    {
	    $qudao=$this->qudao();//调用渠道数据
		$this->assign('qudao',$qudao);
		return $this->fetch();
    }
//	渠道统计
	public function get_list_index1(){
		 $page=input('page');
         $limit= input('limit');
         //搜索条件
         $StartTime=input('StartTime');
		 $EndTime=input('EndTime');
		 if($StartTime && $EndTime){
		 	if($EndTime>=$StartTime){
				$time=$EndTime;
		 		$EndTime= strtotime($EndTime);
				 $StartTime= strtotime($StartTime);
				 $days=$EndTime-$StartTime;
				 $days=$days/86400;
				 $days=$days+1;
//				 p($days);die;
				 if($days<=0){
				 	$days='10';
				 }
		 	}else{
		 		$time=date("Y-m-d");
			    $days='10';
		 	}
		 }else{
		 	$time=date("Y-m-d");
			$days='10';
		 }
         $re=dates_range1($time,$days);
         foreach ($re as $k => $v) {
        	$Time1=$v['times'].' 00:00:00';
			$Time2=$v['times'].' 23:59:59';
//			注册人数
			$where_xb_mem_info['RegTime']=['between',[$Time1,$Time2]];
			$xb_mem_info_count=Db::table('xb_mem_info')->where($where_xb_mem_info)->count('ID');
			$re[$k]['zhuce_count']=$xb_mem_info_count;//注册人数
//			身份证认证
			$xb_renzen_cards_count=Db::table('xb_renzen_cards a')->where($where_xb_mem_info)
   			->join('xb_mem_info b','a.UserID=b.ID','LEFT')
		    ->count('a.ID');
			$re[$k]['shenfenzheng_count']=$xb_renzen_cards_count;//身份证认证
//			手机认证
			$xb_renzen_mobile_count=Db::table('xb_renzen_mobile a')->where($where_xb_mem_info)
   			->join('xb_mem_info b','a.UserID=b.ID','LEFT')
		    ->count('a.ID');
			$re[$k]['shouji_count']=$xb_renzen_mobile_count;//手机认证
//			社交认证
			$xb_renzen_social_count=Db::table('xb_renzen_social a')->where($where_xb_mem_info)
   			->join('xb_mem_info b','a.UserID=b.ID','LEFT')
		    ->count('a.ID');
			$re[$k]['shejiao_count']=$xb_renzen_social_count;//社交认证
//			银行认证
			$xb_renzen_bank_count=Db::table('xb_renzen_bank a')->where($where_xb_mem_info)
   			->join('xb_mem_info b','a.UserID=b.ID','LEFT')
		    ->count('a.ID');
			$re[$k]['yinhang_count']=$xb_renzen_bank_count;//银行认证
//			申请人数
            $xb_renzen_applylist_count=Db::table('xb_loans_applylist a')->where($where_xb_mem_info)
   			->join('xb_mem_info b','a.UserID=b.ID')
			->select();
//		    $key='UserID';
//          $xb_renzen_applylist_count=assoc_unique($xb_renzen_applylist_count, $key);//对二维数组去重处理
			$re[$k]['shenqing_count']=count($xb_renzen_applylist_count);//申请人数
//			申请率
			if($re[$k]['zhuce_count']=='0'){
				$re[$k]['shenqinglv_count']='0.00%';
			}else{
                $re[$k]['shenqinglv_count']=$re[$k]['shenqing_count']/$re[$k]['zhuce_count'];
				$re[$k]['shenqinglv_count']=$re[$k]['shenqinglv_count']*100;
				if(strlen($re[$k]['shenqinglv_count'])>5){
					$re[$k]['shenqinglv_count']=substr($re[$k]['shenqinglv_count'] , 0 , 5);
				}
				
				$re[$k]['shenqinglv_count']=$re[$k]['shenqinglv_count'].'%';
			}
//			放款人数
            $where['RegTime']=['between',[$Time1,$Time2]];//注册时间
			$where['LoanStatus']=array('in',[2,3]);//订单状态
            $xb_renzen_applylist_fankuan_count=Db::table('xb_loans_applylist a')->where($where)
   			->join('xb_mem_info b','a.UserID=b.ID')
			->select();
		    $key='UserID';
            $xb_renzen_applylist_fangkuan_count=assoc_unique($xb_renzen_applylist_fankuan_count, $key);
			$re[$k]['fangkuan_count']=count($xb_renzen_applylist_fangkuan_count);//放款人数
//			放款率
			if($re[$k]['shenqing_count']=='0'){
				$re[$k]['fangkuanlv_count']='0.00%';
			}else{
				$re[$k]['fangkuanlv_count']= sprintf("%.2f", (($re[$k]['fangkuan_count']/$re[$k]['zhuce_count'])*100)) . '%';
			}
		 }
		$cuont=count($re);
        $star=($page-1)*$limit;//开始截取位置
		$re=array_slice($re,$star,$limit);//分页处理
	    songsong($re,$cuont);
	}
//渠道统计详情页面  当天
    public function detailss(){
    	$times= input('ID');//日期
    	$this->assign('times',$times);
    	return $this->fetch();
    }
	//渠道统计详情页面  总转化
    public function zongss(){
    	$times= input('ID');//日期
    	$this->assign('times',$times);
    	return $this->fetch();
    }
	//渠道统计详情页面  渠道费用
    public function qudaomoneyss(){
    	$times= input('ID');//日期
    	$this->assign('times',$times);
    	return $this->fetch();
    }
	//渠道统计详情    当天
    public function detail_list(){
    	$page=input('page');
		$limit= input('limit');
		$times=input('times');
//		 获取所有渠道
		$re=Db::table('xb_tg_admin')->select();
		foreach ($re as $k => $v) {
			$re[$k]['times']=$times;//日期
        	$Time1=$times.' 00:00:00';//拼接当天开始时间
			$Time2=$times.' 23:59:59';//拼接当天结束时间
//			注册人数
			$where_xb_mem_info['RegTime']=['between',[$Time1,$Time2]];
			$where_xb_mem_info['TgadminID']=$v['ID'];
			$xb_mem_info_count=Db::table('xb_mem_info b')->where($where_xb_mem_info)
			->join('xb_tg_admin c','b.TgadminID=c.ID',"LEFT")
			->count('b.ID');
			$re[$k]['zhuce_count']=$xb_mem_info_count;//注册人数
//			身份证认证
			$xb_renzen_cards_count=Db::table('xb_renzen_cards a')->where($where_xb_mem_info)
   			->join('xb_mem_info b','a.UserID=b.ID','LEFT')
			->join('xb_tg_admin c','b.TgadminID=c.ID',"LEFT")
		    ->count('a.ID');
			$re[$k]['shenfenzheng_count']=$xb_renzen_cards_count;//身份证认证
//			手机认证
			$xb_renzen_mobile_count=Db::table('xb_renzen_mobile a')->where($where_xb_mem_info)
   			->join('xb_mem_info b','a.UserID=b.ID','LEFT')
			->join('xb_tg_admin c','b.TgadminID=c.ID',"LEFT")
		    ->count('a.ID');
			$re[$k]['shouji_count']=$xb_renzen_mobile_count;//手机认证
//			社交认证
			$xb_renzen_social_count=Db::table('xb_renzen_social a')->where($where_xb_mem_info)
   			->join('xb_mem_info b','a.UserID=b.ID','LEFT')
			->join('xb_tg_admin c','b.TgadminID=c.ID',"LEFT")
		    ->count('a.ID');
			$re[$k]['shejiao_count']=$xb_renzen_social_count;//社交认证
//			银行认证
			$xb_renzen_bank_count=Db::table('xb_renzen_bank a')->where($where_xb_mem_info)
   			->join('xb_mem_info b','a.UserID=b.ID','LEFT')
			->join('xb_tg_admin c','b.TgadminID=c.ID',"LEFT")
		    ->count('a.ID');
			$re[$k]['yinhang_count']=$xb_renzen_bank_count;//银行认证
//			申请人数
            $xb_renzen_applylist_count=Db::table('xb_loans_applylist a')->where($where_xb_mem_info)
   			->join('xb_mem_info b','a.UserID=b.ID')
			->join('xb_tg_admin c','b.TgadminID=c.ID')
			->select();
		    $key='UserID';
            $xb_renzen_applylist_count=assoc_unique($xb_renzen_applylist_count, $key);
			$re[$k]['shenqing_count']=count($xb_renzen_applylist_count);//申请人数
//			申请率
			if($re[$k]['zhuce_count']=='0'){
				$re[$k]['shenqinglv_count']='0.00%';
			}else{
                $re[$k]['shenqinglv_count']=$re[$k]['shenqing_count']/$re[$k]['zhuce_count'];
				$re[$k]['shenqinglv_count']=$re[$k]['shenqinglv_count']*100;
				if(strlen($re[$k]['shenqinglv_count'])>5){
					$re[$k]['shenqinglv_count']=substr($re[$k]['shenqinglv_count'] , 0 , 5);
				}
				if($re[$k]['shenqinglv_count']=='0'){
					$re[$k]['shenqinglv_count']='0.00%';
				}else{
				  $re[$k]['shenqinglv_count']=$re[$k]['shenqinglv_count'].'%';
				}
			}
//			放款人数
            $where['RegTime']=['between',[$Time1,$Time2]];//注册时间
			$where['LoanStatus']=array('in',[2,3]);//订单状态
			$where['TgadminID']=$v['ID'];
            $xb_renzen_applylist_fankuan_count=Db::table('xb_loans_applylist a')->where($where)
   			->join('xb_mem_info b','a.UserID=b.ID')
			->join('xb_tg_admin c','b.TgadminID=c.ID')
			->select();
//			p($xb_renzen_applylist_fankuan_count);die;
		    $key='UserID';
            $xb_renzen_applylist_fangkuan_count=assoc_unique($xb_renzen_applylist_fankuan_count, $key);
			$re[$k]['fangkuan_count']=count($xb_renzen_applylist_fangkuan_count);//放款人数
//          放款率
			if($re[$k]['shenqing_count']=='0'){
				$re[$k]['fangkuanlv_count']='0.00%';
			}else{
                $re[$k]['fangkuanlv_count']=$re[$k]['fangkuan_count']/$re[$k]['zhuce_count'];
				$re[$k]['fangkuanlv_count']=$re[$k]['fangkuanlv_count']*100;
				if(strlen($re[$k]['fangkuanlv_count'])>5){
					$re[$k]['fangkuanlv_count']=substr($re[$k]['fangkuanlv_count'] , 0 , 5);
				}
				if($re[$k]['fangkuanlv_count']=='0'){
				    $re[$k]['fangkuanlv_count']='0.00%';
				}else{
				    $re[$k]['fangkuanlv_count']=$re[$k]['fangkuanlv_count'].'%';
				}
			}
		}
        $cuont=count($re);
        $star=($page-1)*$limit;//开始截取位置
		$re=array_slice($re,$star,$limit);//分页处理
	    songsong($re,$cuont);
    }

//  渠道统计详情    总转化
    public function zong_list(){
    	$page=input('page');
		$limit= input('limit');
		$times=input('times');
//		 获取所有渠道
		$re=Db::table('xb_tg_admin')->select();
		foreach ($re as $k => $v) {
			$re[$k]['times']=$times;//日期
        	$Time1=$times.' 00:00:00';//拼接当天开始时间
			$Time2=$times.' 23:59:59';//拼接当天结束时间
//			当日注册人数
			$where_xb_mem_info['RegTime']=['between',[$Time1,$Time2]];
			$where_xb_mem_info['TgadminID']=$v['ID'];
			$xb_mem_info_count=Db::table('xb_mem_info b')->where($where_xb_mem_info)
			->join('xb_tg_admin c','b.TgadminID=c.ID',"LEFT")
			->count('b.ID');
			$re[$k]['zhuce_count']=$xb_mem_info_count;//注册人数
//			当日申请人数
            $xb_renzen_applylist_count=Db::table('xb_loans_applylist a')->where($where_xb_mem_info)
   			->join('xb_mem_info b','a.UserID=b.ID')
			->join('xb_tg_admin c','b.TgadminID=c.ID')
			->select();
		    $key='UserID';
            $xb_renzen_applylist_count=assoc_unique($xb_renzen_applylist_count, $key);
			$re[$k]['shenqing_count']=count($xb_renzen_applylist_count);//申请人数
//			申请率
			if($re[$k]['zhuce_count']=='0'){
				$re[$k]['shenqinglv_count']='0.00%';
			}else{
                $re[$k]['shenqinglv_count']=$re[$k]['shenqing_count']/$re[$k]['zhuce_count'];
				$re[$k]['shenqinglv_count']=$re[$k]['shenqinglv_count']*100;
				if(strlen($re[$k]['shenqinglv_count'])>5){
					$re[$k]['shenqinglv_count']=substr($re[$k]['fangkuanlv_count'] , 0 , 5);
				}
				if($re[$k]['shenqinglv_count']=='0'){
					$re[$k]['shenqinglv_count']='0.00%';
				}else{
				  $re[$k]['shenqinglv_count']=$re[$k]['shenqinglv_count'].'%';
				}
			}
//			放款人数
            $where['RegTime']=['between',[$Time1,$Time2]];//注册时间
			$where['LoanStatus']=array('in',[2,3]);//订单状态
			$where['TgadminID']=$v['ID'];
            $xb_renzen_applylist_fankuan_count=Db::table('xb_loans_applylist a')->where($where)
   			->join('xb_mem_info b','a.UserID=b.ID')
			->join('xb_tg_admin c','b.TgadminID=c.ID')
			->select();
//			p($xb_renzen_applylist_fankuan_count);die;
		    $key='UserID';
            $xb_renzen_applylist_fangkuan_count=assoc_unique($xb_renzen_applylist_fankuan_count, $key);
			$re[$k]['fangkuan_count']=count($xb_renzen_applylist_fangkuan_count);//放款人数
//          放款率
			if($re[$k]['shenqing_count']=='0'){
				$re[$k]['fangkuanlv_count']='0.00%';
			}else{
                $re[$k]['fangkuanlv_count']=$re[$k]['fangkuan_count']/$re[$k]['shenqing_count'];
				$re[$k]['fangkuanlv_count']=$re[$k]['fangkuanlv_count']*100;
				if(strlen($re[$k]['fangkuanlv_count'])>5){
					$re[$k]['fangkuanlv_count']=substr($re[$k]['fangkuanlv_count'] , 0 , 5);
				}
				if($re[$k]['fangkuanlv_count']=='0'){
				    $re[$k]['fangkuanlv_count']='0.00%';
				}else{
				    $re[$k]['fangkuanlv_count']=$re[$k]['fangkuanlv_count'].'%';
				}
			}
//			总的  跟上面的一样    
//			总注册人数
//			$where_xb_mem_info['RegTime']=['between',[$Time1,$Time2]];
			$where_xb_mem_info['TgadminID']=$v['ID'];
			$xb_mem_info_count=Db::table('xb_mem_info b')
			->where($where_xb_mem_info)
			->join('xb_tg_admin c','b.TgadminID=c.ID',"LEFT")
			->count('b.ID');
			$re[$k]['zhuce_count_zong']=$xb_mem_info_count;//注册人数
//			总申请人数
            $xb_renzen_applylist_count=Db::table('xb_loans_applylist a')
            ->where($where_xb_mem_info)
   			->join('xb_mem_info b','a.UserID=b.ID')
			->join('xb_tg_admin c','b.TgadminID=c.ID')
			->select();
		    $key='UserID';
            $xb_renzen_applylist_count=assoc_unique($xb_renzen_applylist_count, $key);
			$re[$k]['shenqing_count_zong']=count($xb_renzen_applylist_count);//申请人数
//			总申请率
			if($re[$k]['zhuce_count_zong']=='0'){
				$re[$k]['shenqinglv_count_zong']='0.00%';
			}else{
                $re[$k]['shenqinglv_count_zong']=$re[$k]['shenqing_count_zong']/$re[$k]['zhuce_count_zong'];
				$re[$k]['shenqinglv_count_zong']=$re[$k]['shenqinglv_count_zong']*100;
				if($re[$k]['shenqinglv_count_zong']=='0'){
					$re[$k]['shenqinglv_count_zong']='0.00%';
				}else{
				  $re[$k]['shenqinglv_count_zong']=$re[$k]['shenqinglv_count_zong'].'%';
				}
			}
//			放款人数
//          $where['RegTime']=['between',[$Time1,$Time2]];//注册时间
			$where['LoanStatus']=array('in',[2,3]);//订单状态
			$where['TgadminID']=$v['ID'];
            $xb_renzen_applylist_fankuan_count=Db::table('xb_loans_applylist a')->where($where)
   			->join('xb_mem_info b','a.UserID=b.ID')
			->join('xb_tg_admin c','b.TgadminID=c.ID')
			->select();
		    $key='UserID';
            $xb_renzen_applylist_fangkuan_count=assoc_unique($xb_renzen_applylist_fankuan_count, $key);
			$re[$k]['fangkuan_count_zong']=count($xb_renzen_applylist_fangkuan_count);//放款人数
//          总转化率
			if($re[$k]['shenqing_count_zong']=='0'){
				$re[$k]['fangkuanlv_count_zong']='0.00%';
			}else{
                $re[$k]['fangkuanlv_count_zong']=$re[$k]['fangkuan_count_zong']/$re[$k]['shenqing_count_zong'];
				$re[$k]['fangkuanlv_count_zong']=$re[$k]['fangkuanlv_count_zong']*100;
				if($re[$k]['fangkuanlv_count_zong']=='0'){
				    $re[$k]['fangkuanlv_count_zong']='0.00%';
				}else{
				    $re[$k]['fangkuanlv_count_zong']=$re[$k]['fangkuanlv_count_zong'].'%';
				}
			}
		}
        $cuont=count($re);
        $star=($page-1)*$limit;//开始截取位置
		$re=array_slice($re,$star,$limit);//分页处理
	    songsong($re,$cuont);
    }
	//渠道统计详情    渠道计费
    public function qudaomoney_list(){
    	$page=input('page');
		$limit= input('limit');
		$times=input('times');
//		 获取所有渠道
		$re=Db::table('xb_tg_admin')->select();
		foreach ($re as $k => $v) {
			$re[$k]['times']=$times;//日期
        	$Time1=$times.' 00:00:00';//拼接当天开始时间
			$Time2=$times.' 23:59:59';//拼接当天结束时间
//			当日注册人量
			$where_xb_mem_info['RegTime']=['between',[$Time1,$Time2]];
			$where_xb_mem_info['TgadminID']=$v['ID'];
			$xb_mem_info_count=Db::table('xb_mem_info b')->where($where_xb_mem_info)
			->join('xb_tg_admin c','b.TgadminID=c.ID',"LEFT")
			->count('b.ID');
			$re[$k]['zhuce_count']=$xb_mem_info_count;//日注册量
//			结算量=注册量-扣量比例
//			$Kouliang=$v['Kouliang']/100;
//			$Kouliang=1-$Kouliang;
//          $re[$k]['jiesuan_count']=$re[$k]['zhuce_count']*$Kouliang;//日结算量
            $re[$k]['jiesuan_count']=$re[$k]['zhuce_count'];//日结算量     
            $re[$k]['TuiGuangFangShi']=$v['TuiGuangFangShi'];//推广方式
            $re[$k]['DanJia']=$v['DanJia'].'元';//结算价格
            $re[$k]['ri_money']=$re[$k]['DanJia']*$re[$k]['jiesuan_count'].'元';//日结算金额=日结算量*结算价格
//          累计结算量
            $where['TgadminID']=$v['ID'];
            $xb_mem_info_count_zong=Db::table('xb_mem_info b')->where($where)
			->join('xb_tg_admin c','b.TgadminID=c.ID',"LEFT")
			->count('b.ID');
			$re[$k]['zhuce_count_zong']=$xb_mem_info_count_zong;//累计注册量
            $re[$k]['jiesuan_count_zong']=$re[$k]['zhuce_count_zong'];//累计结算量
            $re[$k]['jiesuan_count_zong_money']=$re[$k]['DanJia']*$re[$k]['jiesuan_count_zong'].'元';//累计结算金额
		}
        $cuont=count($re);
        $star=($page-1)*$limit;//开始截取位置
		$re=array_slice($re,$star,$limit);//分页处理
	    songsong($re,$cuont);
//  渠道名称
//日注册量
//日结算量
//推广方式
//结算价格
//日结算金额
//累计结算量
//累计结算金额
    }
//进出账统计
	public function get_list_index2(){
		 $page=input('page');
         $limit= input('limit');
         //搜索条件
         $StartTime=input('StartTime');
		 $EndTime=input('EndTime');
		 if($StartTime && $EndTime){
		 	if($EndTime>=$StartTime){
				$time=$EndTime;
		 		$EndTime= strtotime($EndTime);
				 $StartTime= strtotime($StartTime);
				 $days=$EndTime-$StartTime;
				 $days=$days/86400;
				 $days=$days+1;
//				 p($days);die;
				 if($days<=0){
				 	$days='10';
				 }
		 	}else{
		 		$time=date("Y-m-d");
			    $days='10';
		 	}
		 }else{
		 	$time=date("Y-m-d");
			$days='10';
		 }
         $re=dates_range1($time,$days);

         foreach ($re as $k => $v) {
//       	支付方式（放款方式） 1微信 2支付宝 3银行卡(后台手动放款)4银行卡(自动放款)
//          出账统计  订单表中计算放放款总金额就可以了
        	$Time1=$v['times'].' 00:00:00';
			$Time2=$v['times'].' 23:59:59';
			$where_xb_loans_applylist_laoke['OpenTime']=['between',[$Time1,$Time2]];
			$xb_loans_applylist_count_laoke=Db::table('xb_loans_applylist')->where($where_xb_loans_applylist_laoke)->field('AdoptMoney,pay,ApplyMoney')->select();
			if($xb_loans_applylist_count_laoke){
				$chuzhang='0';//总
				$chuzhang_weixin='0';//微信
				$chuzhang_zhifubao='0';//支付宝
				$chuzhang_yinhang='0';//银行 手动
				$chuzhang_yinhang2='0';//银行 自动
				foreach ($xb_loans_applylist_count_laoke as $kk => $vv) {
					if($vv['pay']=='1'){
						$chuzhang_weixin=$chuzhang_weixin+$vv['AdoptMoney'];
					}
					if($vv['pay']=='2'){
						$chuzhang_zhifubao=$chuzhang_zhifubao+$vv['AdoptMoney'];
					}
					if($vv['pay']=='3'){
						$chuzhang_yinhang=$chuzhang_yinhang+$vv['AdoptMoney'];
					}
					if($vv['pay']=='4'){
						$chuzhang_yinhang2=$chuzhang_yinhang2+$vv['AdoptMoney'];
					}
					$chuzhang=$chuzhang+$vv['AdoptMoney'];
				}
				$re[$k]['chuzhang']=$chuzhang.'元';
				if($chuzhang_weixin>0){
					$re[$k]['chuzhang_weixin']=$chuzhang_weixin.'元';
				}else{
					$re[$k]['chuzhang_weixin']='0元';
				}
				if($chuzhang_zhifubao>0){
					$re[$k]['chuzhang_zhifubao']=$chuzhang_zhifubao.'元';
				}else{
					$re[$k]['chuzhang_zhifubao']='0元';
				}
				if($chuzhang_yinhang>0){
					$re[$k]['chuzhang_yinhang']=$chuzhang_yinhang.'元';
				}else{
					$re[$k]['chuzhang_yinhang']='0元';
				}
				if($chuzhang_yinhang2>0){
					$re[$k]['chuzhang_yinhang2']=$chuzhang_yinhang2.'元';
				}else{
					$re[$k]['chuzhang_yinhang2']='0元';
				}
			}else{
				$re[$k]['chuzhang']='0元';
				$re[$k]['chuzhang_weixin']='0元';
				$re[$k]['chuzhang_zhifubao']='0元';
				$re[$k]['chuzhang_yinhang']='0元';
				$re[$k]['chuzhang_yinhang2']='0元';
				
			}
//			进账统计  支付记录表计算总金额就可以了   豁免的不做统计
//		 还款方式 1线下微信 2线下支付宝 3线下银联 4线上微信 5线上支付宝 6线上银联 7豁免 
			$where_xb_loans_paylist['CreateTime']=['between',[$Time1,$Time2]];
			$where_xb_loans_paylist['pay']=array('in',[1,2,3,4,5,6]);
			$xbloans_paylist=Db::table('xb_loans_paylist')->where($where_xb_loans_paylist)->field('OrderAmount,pay')->select();
			if($xbloans_paylist){
//				p($xbloans_paylist);
				$jinzhang='0';//总
				$jinzhang_weixin='0';//线下微信
				$jinzhang_zhifubao='0';//线下支付宝
				$jinzhang_yinhang='0';//线下银行
				$jinzhang_weixin2='0';//线下微信
				$jinzhang_zhifubao2='0';//线下支付宝
				$jinzhang_yinhang2='0';//线上银行
				foreach ($xbloans_paylist as $aa => $bb) {
					if($bb['pay']=='1'){
						$jinzhang_weixin=$jinzhang_weixin+$bb['OrderAmount'];
					}
					if($bb['pay']=='2'){
						$jinzhang_zhifubao=$jinzhang_zhifubao+$bb['OrderAmount'];
					}
					if($bb['pay']=='3'){
						$jinzhang_yinhang=$jinzhang_yinhang+$bb['OrderAmount'];
					}
					if($bb['pay']=='4'){
						$jinzhang_weixin2=$jinzhang_weixin2+$bb['OrderAmount'];
					}
					if($bb['pay']=='5'){
						$jinzhang_zhifubao2=$jinzhang_zhifubao2+$bb['OrderAmount'];
					}
					if($bb['pay']=='6'){
						$jinzhang_yinhang2=$jinzhang_yinhang2+$bb['OrderAmount'];
					}
					$jinzhang=$jinzhang+$bb['OrderAmount'];
				}
				$re[$k]['jinzhang']=$jinzhang.'元';
				$re[$k]['jinzhang_weixin']=$jinzhang_weixin.'元';
				$re[$k]['jinzhang_zhifubao']=$jinzhang_zhifubao.'元';
				$re[$k]['jinzhang_yinhang']=$jinzhang_yinhang.'元';
				$re[$k]['jinzhang_weixin2']=$jinzhang_weixin2.'元';
				$re[$k]['jinzhang_zhifubao2']=$jinzhang_zhifubao2.'元';
				$re[$k]['jinzhang_yinhang2']=$jinzhang_yinhang2.'元';
			}else{
				$re[$k]['jinzhang']='0元';
				$re[$k]['jinzhang_weixin']='0元';
				$re[$k]['jinzhang_zhifubao']='0元';
				$re[$k]['jinzhang_yinhang']='0元';
				$re[$k]['jinzhang_weixin2']='0元';
				$re[$k]['jinzhang_zhifubao2']='0元';
				$re[$k]['jinzhang_yinhang2']='0元';
				
			}
        }
        $data_re=$re;
		$cuont=count($re);
		$star=($page-1)*$limit;//开始截取位置
		$re=array_slice($data_re,$star,$limit);
        $jinzhang_zong='0';
		$jinzhang_zong_weixin='0';
		$jinzhang_zong_zhifubao='0';
	    $jinzhang_zong_yinhang='0';
		$jinzhang_zong_weixin2='0';
		$jinzhang_zong_zhifubao2='0';
		$jinzhang_zong_yinhang2='0';
     	$chuzhang_zong='0';
		$chuzhang_zong_weixin='0';
		$chuzhang_zong_zhifubao='0';
		$chuzhang_zong_yinhang='0';
		$chuzhang_zong_yinhang2='0';
//		p($data_re);die;
		foreach ($data_re as $k => $v) {
			$jinzhang_zong=$jinzhang_zong+$v['jinzhang'];
			$jinzhang_zong_weixin=$jinzhang_zong_weixin+$v['jinzhang_weixin'];
			$jinzhang_zong_zhifubao=$jinzhang_zong_zhifubao+$v['jinzhang_zhifubao'];
			$jinzhang_zong_yinhang=$jinzhang_zong_yinhang+$v['jinzhang_yinhang'];
			$jinzhang_zong_weixin2=$jinzhang_zong_weixin2+$v['jinzhang_weixin2'];
			$jinzhang_zong_zhifubao2=$jinzhang_zong_zhifubao2+$v['jinzhang_zhifubao2'];
			$jinzhang_zong_yinhang2=$jinzhang_zong_yinhang2+$v['jinzhang_yinhang2'];
			$chuzhang_zong=$chuzhang_zong+$v['chuzhang'];
			$chuzhang_zong_weixin=$chuzhang_zong_weixin+$v['chuzhang_weixin'];
			$chuzhang_zong_zhifubao=$chuzhang_zong_zhifubao+$v['chuzhang_zhifubao'];
			$chuzhang_zong_yinhang=$chuzhang_zong_yinhang+$v['chuzhang_yinhang'];
			$chuzhang_zong_yinhang2=$chuzhang_zong_yinhang2+$v['chuzhang_yinhang2'];
			
		}
		$zhuijia['jinzhang']=$jinzhang_zong.'元';
		$zhuijia['jinzhang_weixin']=$jinzhang_zong_weixin.'元';
		$zhuijia['jinzhang_zhifubao']=$jinzhang_zong_zhifubao.'元';
        $zhuijia['jinzhang_yinhang']=$jinzhang_zong_yinhang.'元';
		$zhuijia['jinzhang_weixin2']=$jinzhang_zong_weixin2.'元';
		$zhuijia['jinzhang_zhifubao2']=$jinzhang_zong_zhifubao2.'元';
		$zhuijia['jinzhang_yinhang2']=$jinzhang_zong_yinhang2.'元';
		$zhuijia['chuzhang']=$chuzhang_zong.'元';
		$zhuijia['chuzhang_weixin']=$chuzhang_zong_weixin.'元';
		$zhuijia['chuzhang_zhifubao']=$chuzhang_zong_zhifubao.'元';
		$zhuijia['chuzhang_yinhang']=$chuzhang_zong_yinhang.'元';
		$zhuijia['chuzhang_yinhang2']=$chuzhang_zong_yinhang2.'元';
		$zhuijia['times']='查询时间段总计';
        $re[]=$zhuijia;
		
//		p($re);
		songsong($re,$cuont);
	}


	//个人对账页面
	public function checkAccount(){

		//获取催收人员数据并分配页面当筛选条件
		$callRoleData = db('sys_administrator')->where(['IsDel' => 0,'Status' => 1])->where('RoleID','in','51,54')->select();
		$this->assign('callRoleData',$callRoleData);

		return $this->fetch();		
	}
	//个人对账页面数据
	public function getCheckAcount()
	{
		$postData = input('');
		$page = $postData['page'] ?? 1;
		$limit = $postData['limit'] ?? 10;

        $where=[];
		if(isset($postData['LoanStatus']) && !empty($postData['LoanStatus']))
        {
            $where['a.LoanStatus'] = $postData['LoanStatus'];
        }else{
			$where['a.LoanStatus'] = ['in','2,3'];
		}
        if(isset($postData['TrueName']) && !empty($postData['TrueName']))
        {
            $where['i.TrueName'] = $postData['TrueName'];
        }
        if(isset($postData['Mobile']) && !empty($postData['Mobile']))
        {
            $where['i.Mobile'] = $postData['Mobile'];
        }
        if(isset($postData['IDCard']) && !empty($postData['IDCard']))
        {
            $where['i.IDCard'] = $postData['IDCard'];
		}
		if(isset($postData['cuishourenID']) && !empty($postData['cuishourenID']))
        {
            $where['a.cuishourenID'] = $postData['cuishourenID'];
		}else{
			$where['a.cuishourenID'] = ['<>','not null'];
		}
		if(isset($postData['YyFkTime']) && !empty($postData['YyFkTime']))
        {
			$time = explode(' - ', $postData['YyFkTime']);
			$where['a.YyFkTime'] = ['between', $time[0].','.$time[1].' 23:59:59'];
		}else{
			$where['a.YyFkTime'] = ['< time', date('Y-m-d')];
		}

		//TP查询，没加h.HkTime限制时间 下面原生SQL是加了 这里
		$data = db('loans_applylist')->alias('a')
			->join('xb_mem_info i','a.UserID = i.ID','LEFT')
			->join('xb_sys_administrator sa','a.cuishourenID = sa.ID','LEFT')
			->where($where)
			->field(['a.ID,a.ApplyTime,a.cuishourenID,i.TrueName,i.Mobile,i.IDCard,a.OrderSn,a.ApplyMoney,a.ApplyDay,a.YyFkTime,a.daoqiMoney,a.yuqililv,sa.UserName'])
			->group('a.ID')
			->order('a.ID','desc')
			->page($page,$limit)
			->select();

		//获取系统限制时间段
		$limitTime = (new System())->getSystemInfo(false);
		$limitTime = explode('-',$limitTime['repayment_time']);
		if(isset($limitTime[0]) && isset($limitTime[1])){
			$limitStartTime = '09:00:00';
			$limitEndTime = '21:00:00';
		}else{
			$limitStartTime = $limitTime[0];
			$limitEndTime = $limitTime[1];
		}

		$orderObj = new Order();
		foreach($data as $k=>$v){
			//实际应还(包含逾期金额等)
			$data[$k]['ShoulPay'] = number_format($orderObj->getOrderShould($v,false),2);
			//豁免金额
			$data[$k]['huomian'] = number_format($orderObj->getOrderReductionMoney($v),2);
			//实际还款金额 取还款时间段为早9点至晚9点
			$temp = db('loans_hklist')->where(['ApplyID' => $v['ID'],'Type' => ['<>',3]])->where("HkTime > DATE_FORMAT(HkTime,'%Y-%m-%d {$limitStartTime}') and HkTime < DATE_FORMAT(HkTime,'%Y-%m-%d {$limitEndTime}')")->field('HkTime,SUM(TotalMoney) as TotalMoney')->find();//用户最终还款金额(不包含豁免金额)
			$data[$k]['shijihuan'] = $temp['TotalMoney']??$temp['TotalMoney'];
			$data[$k]['HkTime'] = $temp['HkTime']??$temp['HkTime'];
			//催收人
			$data[$k]['cuishouren'] = ($v['UserName'])?$v['UserName']:"<span style='color:red' lay-tips='催收人被删除或禁用 催收人ID {$v['cuishourenID']}'>催收人已不存在?</span>";
		}

		$count = db('loans_applylist')->alias('a')
			->join('xb_mem_info i','a.UserID = i.ID','LEFT')
			->where($where)
			->group('a.ID')
			->count('a.ID');
			
        if($data){
           	songsong($data,$count);  
        }else{
            songsong();
        }
	}


	
}
